﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Marmat.Forms
{
    public class Band_Form_Designer : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            IDesignerHost designerHost = (IDesignerHost)context.GetService(typeof(IDesignerHost));
            if (edSvc is object)
            {
                try
                {
                    BaseGrid Grid = (BaseGrid)context.Instance;
                    var frm = new BandCollectionEditorForm(Grid, (Views.Grid.BandCollection)value);
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        Grid.Headers.Clear();
                        this.ForCollection((Views.Grid.BandCollection)value, frm.BandedGridView1.Bands);
                        context.OnComponentChanged();
                    }
                    else
                    {
                        Helper.UITypeEditor.ComponentChangeService((Control)context.Instance, context.PropertyDescriptor.Name, false);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return value;
        }

        internal void ForCollection(Views.Grid.BandCollection Collection, DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection Bands)
        {
            foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand item in Bands)
            {
                Views.Grid.Band Band = (Views.Grid.Band)item.Tag;
                if (Band is object && Collection.Contains(Band) == false)
                {
                    Collection.Add(Band);
                    Band.Columns.Clear();
                    if (item.Columns.Count > 0)
                    {
                        foreach (DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn GridBand in item.Columns)
                        {
                            if (GridBand.Visible == true)
                            {
                                Views.Grid.Column Column = (Views.Grid.Column)GridBand.Tag;
                                Band.Columns.Add(Column);
                            }
                        }
                    }

                    if (item.Children.Count > 0)
                    {
                        this.ForCollection(Band.Bands, item.Children);
                    }
                }
            }
        }

        // Friend Sub ForCollection2(ByVal Collection As  Marmat.Forms.BaseControl.Grid.BandCollection, ByVal Bands As DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection)
        // For Each item As  Marmat.Forms.BaseControl.Grid.Band In Collection
        // If Collection.Contains(Band) = False Then
        // Collection.Add(Band)
        // If item.Children.Count > 0 Then
        // ForCollection(Band.Bands, item.Children)
        // End If
        // End If
        // Next
        // End Sub

    }
}